//
//  ViewController.m
//  FormDemo
//
//  Created by zkingsoft on 16/1/5.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//

#import "ViewController.h"

#import "SingerFormTableViewCell.h"
#import "HeadFormView.h"
#import "LeftView.h"
#import "CornerLineView.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, SingerFormTableViewCellDelegate>

@property (nonatomic, strong) LeftView *leftView;// 左边的视图
@property (nonatomic, strong) CornerLineView  *cornerView;

@property (nonatomic, strong) UIView   *myHeadView;

@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"FORMDEMO";
    // 创建头视图 然后头视图上再放上创建的小得headFormView
    self.myHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*KCOUNT, HEIGHT)];
    self.myHeadView.backgroundColor = [UIColor grayColor];
    
    for (int i = 0; i < KCOUNT; i++) {
        HeadFormView *formHeadView = [[HeadFormView alloc] initWithFrame:CGRectMake(i*WIDTH+1, 0, WIDTH-2, HEIGHT)];
        formHeadView.headContentText = [NSString stringWithFormat:@"HEAD-%d", i];
        formHeadView.backgroundColor = [UIColor cyanColor];
        [self.myHeadView addSubview:formHeadView];
    }
    
    // 创建myTableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*KCOUNT, KHEIGHT-64) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = HEIGHT;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    // myScrollView
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH*0.7, 0, KWIDTH-WIDTH*0.7, KHEIGHT)];
    [myScrollView addSubview:self.myTableView];
    myScrollView.bounces = NO;
    myScrollView.contentSize = CGSizeMake(self.myHeadView.frame.size.width, 0);
    [self.view addSubview:myScrollView];
    
    
    // leftView
    self.leftView = [[LeftView alloc] initWithFrame:CGRectMake(0, 64, WIDTH*0.7, KHEIGHT-64)];
    [self.view addSubview:self.leftView];
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return KCOUNT;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld%ld", indexPath.section, indexPath.row];
    
    SingerFormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[SingerFormTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.contentView.backgroundColor = [UIColor grayColor];
        cell.delegate = self;
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.myHeadView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT;
}


#pragma mark - 当form点击的时候调用
- (void)FormTableViewCellIsClicked:(SingerFormTableViewCell *)tableViewCell withFormView:(SingerFormView *)formView{
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:tableViewCell];
    NSLog(@"======%@------%@=====%ld", indexPath, formView.formContentText, formView.listRow);
    
    formView.backgroundColor = [self ChangeBackGroundColor];
    
    // alert 显示 8.0
    NSString *msg = [NSString stringWithFormat:@"行与列 = {%ld,%ld}, FormView = %@", indexPath.row, formView.listRow, formView.formContentText];
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"tips" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertView addAction:OKAction];
    [self presentViewController:alertView animated:YES completion:nil];
}

#pragma mark - 联动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat tableViewOffSetY = self.myTableView.contentOffset.y;// form的y方向偏移量
    
    CGPoint leftFormOffSet = self.leftView.leftTableView.contentOffset;// leftForm的偏移坐标
    
    leftFormOffSet.y = tableViewOffSetY;
    
    self.leftView.leftTableView.contentOffset = leftFormOffSet;// 让左边的tableView的偏移 随着 右边form的偏移而偏移
    
    if (tableViewOffSetY == 0) {
        self.leftView.leftTableView.contentOffset = CGPointZero;
    }
}

// 随机一个颜色
- (UIColor *)ChangeBackGroundColor{
    CGFloat red = arc4random()%256;
    CGFloat blue = arc4random()%256;
    CGFloat green = arc4random()%256;
    
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
    
    return color;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




